﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace ZhangZhao.WebAPI
{
    public partial class UserContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = string.Empty;

            string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
            string Output = HttpContext.Current.Request[Parameter.Output];

            if (!string.IsNullOrWhiteSpace(UserName))
            {
                ZhangZhao.BLL.H_UserContact bll = new ZhangZhao.BLL.H_UserContact();

                #region 获取联系信息
                ZhangZhao.Model.H_UserContact Contact = bll.GetModel(UserName);

                if (Contact != null)
                {
                    if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {
                        s = JsonConvert.SerializeObject(Contact);
                    }
                    else
                    {
                        using (MemoryStream Stream = new MemoryStream())
                        {
                            XmlSerializer xml = new XmlSerializer(typeof(ZhangZhao.Model.H_UserContact));

                            try
                            {
                                xml.Serialize(Stream, Contact);
                            }
                            catch { }

                            Stream.Position = 0;
                            using (StreamReader sr = new StreamReader(Stream))
                            {
                                s = sr.ReadToEnd();
                            }

                            xml = null;
                        }
                    }
                }
                #endregion
            }

            Response.Clear();
            Response.Write(s);
            Response.End();
        }
    }
}